package com.liyang.springcloud.controller;

import com.liyang.springcloud.common.entities.CommonResult;
import com.liyang.springcloud.common.entities.Payment;
import com.liyang.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: cloud-2024
 * @BelongsPackage: com.liyang.springcloud.controller
 * @Author: liyang
 * @CreateTime: 2023-12-19  19:58
 * @Description: 支付 控制层
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private PaymentService paymentService;

    /**
     * @description: 支付  插入数据
     * @author: liyang
     * @date: 2023/12/19 20:03
     * @param: payment
     * @return: com.liyang.springcloud.entities.CommonResult<java.lang.Integer>
     **/
    @PostMapping("/create")
    public CommonResult<Integer> create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        if (result > 0) {
            return new CommonResult<>(200, "插入成功", result);
        } else {
            return new CommonResult<>(500, "插入失败");
        }
    }

    /**
     * @description: 支付  通过Id查询详情
     * @author: liyang
     * @date: 2023/12/19 20:06
     * @param: id
     * @return: com.liyang.springcloud.entities.CommonResult<com.liyang.springcloud.entities.Payment>
     **/
    @GetMapping("/selectById/{id}")
    public CommonResult<Payment> selectById(@PathVariable("id") Long id) {
        Payment payment = paymentService.selectById(id);
        log.info("****插入结果****");
        log.info("支付" + payment);
        if ( payment != null) {
            return new CommonResult<>(200, "查询成功" + " serverPort : " + serverPort, payment);
        } else {
            return new CommonResult<>(500, "查询失败");
        }
    }

    @GetMapping("/get/lb")
    public String getPort() {
        return  serverPort;
    }
}
